﻿using SerieSpotter.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SerieSpotter.Controllers
{
    public class SeasonsController : LanguageController
    {
        //
        // GET: /Seasons/
        SerieSpotterDb _db = new SerieSpotterDb();

        public ActionResult Index([Bind(Prefix="id")]int Show)
        {
            var season = _db.Shows.Find(Show);
            if (season != null)
            {
                return View(season);
            }
            return HttpNotFound(); 
        }

       
        //
        // GET: /Seasons/Create
        [HttpGet]
        // [Authorize(Roles = "admin")]
        [Authorize(Roles = "admin")] // beide mogen een comment creeëren
        public ActionResult Create(int showId)
        {
            if (showId != 0)
            {
                return View();
            }
            else
            {
                return View("mislukt");
            }

        }

        //
        // POST: /Seasons/Create

        [HttpPost]
        // [Authorize(Roles="admin")]
        [Authorize(Roles = "admin")]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Season season)
        {
            if (ModelState.IsValid)
            {
                _db.Seasons.Add(season); // adden
                _db.SaveChanges(); // saven in db
                return RedirectToAction("Index", new { id = season.ShowId }); // terug naar de overzichtspagina
            }
            return View(season);
        }


        //
        // GET: /Seasons/Edit/5
        [HttpGet]
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            var model = _db.Seasons.Find(id); // haal gegevens op
            return View(model); // toon op scherm
        }

        //
        // POST: /Seasons/Edit/5

        [HttpPost]
        [Authorize(Roles = "admin")]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Season season)
        {
            if (ModelState.IsValid)
            {

                _db.Entry(season).State = EntityState.Modified; // er is iets veranderd!
                _db.SaveChanges(); // opslaan veranderingen
                return RedirectToAction("Index", new { id = season.ShowId }); //redirect
            }
            return View(season);
        }

        //
        // GET: /Seasons/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Seasons/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        //sluit de database connectie
        protected override void Dispose(bool disposing)
        {
            if (_db != null)
                _db.Dispose();
            base.Dispose(disposing);
        }
    }
}
